home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / Mac gperf 1.9.cpt / Mac gperf 1.9 / src / readline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-10  |  3.0 KB  |  121 lines  |  [TEXT/KAHL]

  1. /* Correctly reads an arbitrarily size string.
  2.  
  3.    Copyright (C) 1989 Free Software Foundation, Inc.
  4.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  5.  
  6. This file is part of GNU GPERF.
  7.  
  8. GNU GPERF is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU GPERF is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU GPERF; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include "xmalloc.h"
  24. #include "gperf.h"
  25.  
  26. #include "readline.h"
  27.  
  28.     /* Size of each chunk. */
  29.  
  30. #define CHUNK_SIZE BUFSIZ
  31.  
  32. /*******************************************************************************\
  33. *                              Prototypes                                        *
  34. \*******************************************************************************/
  35.  
  36. static char * readln_aux( int chunks );
  37.  
  38.  
  39.     /***********************************************************************\
  40.     *                                                                        *
  41.     * name:        read_line                                                    *
  42.     *                                                                        *
  43.     * descr:    Return the "next" line, ignoring comments beginning with    *
  44.     *            #. Calls itself until a good line is returned or end        *
  45.     *            of file is reached.                                            *
  46.     *                                                                        *
  47.     \***********************************************************************/
  48.  
  49. char *    read_line() 
  50. {
  51.     int        c;
  52.  
  53.         /*    Skip a line beginning with #  */
  54.  
  55.     if ( ( c = getc( KeyFile ) ) == '#' )
  56.     {
  57.         while ( ( ( c = getc( KeyFile ) ) != '\n' ) && ( c != EOF ) )
  58.             ;
  59.  
  60.         return ( c != EOF ) ? read_line() : NULL;
  61.     }
  62.  
  63.         /*  Return the next line  */
  64.  
  65.     else
  66.     {
  67.         ungetc( c, KeyFile );
  68.         return readln_aux( 0 );
  69.     }
  70. }
  71.  
  72.  
  73.     /***********************************************************************\
  74.     *                                                                        *
  75.     * name:        readln_aux                                                    *
  76.     *                                                                        *
  77.     * descr:    Recursively fills up the buffer.                            *
  78.     *                                                                        *
  79.     \***********************************************************************/
  80.  
  81. static char * readln_aux( int chunks ) 
  82. {
  83.     char                buf[ CHUNK_SIZE ];
  84.     register char *        bufptr = buf;
  85.     register char *        ptr;
  86.     int                    c;
  87.  
  88.         /*
  89.         **    Fill the current buffer. Get chars until EOF or
  90.         **    \n, or buffer is full.
  91.         */
  92.  
  93.     while ( ( c = getc( KeyFile ) ) != EOF && ( c != '\n' ) )
  94.     {
  95.         *bufptr++ = c;
  96.         if ( bufptr - buf >= CHUNK_SIZE)        /* prepend remainder to ptr buffer */
  97.         {
  98.             if ( ptr = readln_aux( chunks + 1 ) )
  99.                 for ( ; bufptr != buf; *--ptr = *--bufptr )
  100.                     ;
  101.  
  102.             return ptr;
  103.         }
  104.     }
  105.  
  106.     if ( c == EOF && bufptr == buf )
  107.         return NULL;
  108.  
  109.     c = ( chunks * CHUNK_SIZE + bufptr - buf ) + 1;
  110.  
  111.     if ( ptr = (char *) xmalloc( c ) )
  112.     {
  113.         for ( *(ptr += (c - 1) ) = '\0'; bufptr != buf; *--ptr = *--bufptr )
  114.             ;
  115.  
  116.         return ptr;
  117.     } 
  118.     else 
  119.         return NULL;
  120. }
  121.